package openapi

import "github.com/getkin/kin-openapi/openapi3"

//Merge 将toMerge合并到base中，注意，本方法并不对base进行拷贝，所有操作会反馈到base的传入对象中
//因为用于独立的merge命令，因此暂不考虑复制base，且暂时只是拷贝paths
func Merge(base, toMerge *openapi3.T) *openapi3.T {
	var ok bool
	var oldMethods *openapi3.PathItem
	for p, methods := range toMerge.Paths {
		oldMethods, ok = base.Paths[p]
		if ok {
			if methods.Get != nil && oldMethods.Get == nil {
				oldMethods.Get = methods.Get
			}
			if methods.Post != nil && oldMethods.Post == nil {
				oldMethods.Post = methods.Get
			}
			if methods.Head != nil && oldMethods.Head == nil {
				oldMethods.Head = methods.Get
			}
			if methods.Put != nil && oldMethods.Put == nil {
				oldMethods.Put = methods.Get
			}
			if methods.Patch != nil && oldMethods.Patch == nil {
				oldMethods.Patch = methods.Get
			}
			if methods.Connect != nil && oldMethods.Connect == nil {
				oldMethods.Connect = methods.Get
			}
			if methods.Delete != nil && oldMethods.Delete == nil {
				oldMethods.Delete = methods.Get
			}
			if methods.Options != nil && oldMethods.Options == nil {
				oldMethods.Options = methods.Get
			}
			if methods.Trace != nil && oldMethods.Trace == nil {
				oldMethods.Trace = methods.Get
			}
		} else {
			base.Paths[p] = methods
		}
	}
	return base
}
